package com.sec.android.app.myfiles.feature.Hover;

import android.content.Context;
import android.provider.MediaStore;

import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.info.FileType;
import com.sec.android.app.myfiles.info.MediaFile;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.local.category.CategoryDbTableInfo;
import com.sec.android.app.myfiles.module.local.category.CategoryFileRecord;
import com.sec.android.app.myfiles.provider.DbTableInfo;

public class CategoryFolderHoverView extends FolderHoverView {
    private static String mDocumentSelection = null;

    public CategoryFolderHoverView(Context context, FileRecord record, AbsMyFilesFragment fragment) {
        super(context, record, fragment);
    }

    @Override
    protected String getSelection() {
        String selection = super.getSelection();
        StringBuilder sb = new StringBuilder();
        sb.append(selection);
        FileRecord.CategoryType categoryType = CategoryFileRecord.getCategoryType(((CategoryFileRecord) mRecord).getRealPath());

        switch (categoryType) {
            case Image:
                sb.append(" AND media_type = ").append(MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE);
                break;
            case Video:
                sb.append(" AND media_type = ").append(MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO);
                break;
            case Audio:
                sb.append(" AND media_type = ").append(MediaStore.Files.FileColumns.MEDIA_TYPE_AUDIO);
                break;
            case Document:
                if (AppFeatures.isSupportDocumentType(mContext)) {
                    sb.append(" AND is_doc = 1");
                } else {
                    sb.append(getDocumentSelection());
                }
                break;
            case Apk:
                sb.append("AND _data LIKE '%.apk'");
                break;
        }

        return sb.toString();
    }

    @Override
    protected String getOrderBy() {
        return "date_modified DESC";
    }

    private static String getDocumentSelection() {
        if (mDocumentSelection == null) {
            String[] whereArgs = MediaFile.getDocumentExtensions();
            int length = whereArgs.length;
            for (int i = 0; i < length; i++) {
                whereArgs[i] = "'%." + whereArgs[i] + "'";
            }

            final String pathColumn = CategoryDbTableInfo.getInstance().getColumnName(DbTableInfo.COLUMN_ID.PATH);
            final String formatColumn = CategoryDbTableInfo.getInstance().getColumnName(DbTableInfo.COLUMN_ID.FORMAT);
            StringBuilder sb = new StringBuilder();
            sb.append(" AND ");
            sb.append('(');
            for (int i = 0; i < length; i++) {
                if (i < length - 1) {
                    sb.append(pathColumn).append(" LIKE ").append(whereArgs[i]).append(" OR ");
                } else {
                    sb.append(pathColumn).append(" LIKE ").append(whereArgs[i]);
                }
            }
            sb.append(')').append(" AND ").append(formatColumn).append(" != ").append(FileType.FOLDER);
            mDocumentSelection = sb.toString();
        }
        return mDocumentSelection;
    }
}
